<?php
defined ( 'SYSPATH' ) or die ( 'No direct access allowed.' );
/**
 *
 *让步接收的打印
 * handlequality
 * 
 * @author cxy
 *        
 */
class Controller_concession extends Controller
{
protected $user;//登陆用户的ID
	protected $recieve_man;//登陆用户的名字


    public function before()
    {
    	$userid=User::active_user()->id;
		$this->user=$userid;
       	$username=ORM::factory('user')->where('id', '=', $userid)->find();
    	$this->recieve_man= $username->name;
    	try {
    		  ACL::Required('concession da');
    	} catch ( HTTP_Exception_403 $e ) {
				Message::error('sorry，你没有权限进这个目录');
    		$this->request->redirect ( URL::base (true) );
    	}
        parent::before();
    }
	//打印报告单
	public function action_index()
	{
		$this->title='不良品处理打印报告';
		
		$db=DB::select()->from('bad_product_delivery')->where('id','=',$_GET['id'])->execute('newerp')->current();
		$time = date('Ymd');
		$pur_evidence=$db['pur_evidence'];
		$print_status=$db['print_status'];//打印状态
		//加一个计划员20140722
		if(isset($db['id'])){
			$db['operate_man']=Erpuser::nick_by_id($db['operate_man']);
		}else{
			$db['operate_man']='';
		}
		if(empty($pur_evidence))
		{
			$wh = ' pur_evidence > '.$time;
			$feild = 'MAX(pur_evidence) AS maxid';
			$max = qualitycomm::qualityinfo($wh,$feild);
			$max = trim($max['0']['maxid']);
			
	
		//	echo $pur_evidence;exit;
			$evidence = '';
			if(empty($max))
			{
				$evidence = $time.'0001';
			}
			else
			{
				$time1 = substr($max,0,8);
				$int = substr($max,-4);
				if($time == $time1){
					$int = $int + 1;
					$len = '';
					for($i = strlen($int);$i <4 ;$i++){
						$len .= '0';
					}
					$evidence = $time.$len.$int;
				}else{
					$evidence = $time.'0001';
				}
				
			}
			$set = 'print_status=1,pur_evidence='.$evidence;
			$table='bad_product_delivery';
			$where = ' id ='.$_GET['id'];
			qualityaction::updatequality($set,$where,$table);
		}
		else 
		{
			$evidence=$pur_evidence;
		}
		$userId=USER::Active_user()->id;
		$user_name=Erpuser::nick_by_id($userId);
		$view=View::factory('unqualified/printquality');
		$view->evidence=$evidence;
		$view->result=$db;
		$check_detail_id = $db['check_detail_id'];
		$recieve_detail_id =DB::select()->from('check_quality_details')->where('id','=',$check_detail_id)->execute('newerp')->get('recieve_detail_id');
		$check =DB::select()->from('check_quality_details')->where('id','=',$check_detail_id)->execute('newerp')->current();
		$check['unquality_count'] = $db['quantity'];
		$view->check = $check;
		$view->set('user_name',$user_name);
		$this->response->body($view);
		

	}
}
